package org.example.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class BubbleSort {

    public static void main(String[] args) {
        int arr[] = {3, 9, -1, 10, 20};
       /* int[] arr = new int[50000];
        for (int i = 0; i < 50000; i++){
            arr[i] = (int) (Math.random()*8000000);
        }
        System.out.println("当前时间：" + System.currentTimeMillis());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));*/
        bubbleSort(arr);
        //System.out.println("计算后的时间：" + System.currentTimeMillis());


    }

    public static void bubbleSort(int[] arr){
        int temp = 0;
        //设置标记
        boolean flag = false;
        for (int i = 0; i < arr.length -1; i++){
            for (int j = 0; j < arr.length - 1 -i; j++){
                if (arr[j] > arr[j+1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
            //如果flag为false，则说明数组已经有序，不用在遍历
            if (!flag){
                break;
            }
        }
        System.out.println(Arrays.toString(arr));
    }


}
